<template>
  <div class="accepted-tasks">
    <el-card>
      <template #header>
        <span>已接受任务</span>
      </template>
      
      <el-table :data="tasks" v-loading="loading">
        <el-table-column prop="title" label="任务标题" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="reward" label="奖励" width="100">
          <template #default="{ row }">
            ¥{{ row.reward }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="接受时间" width="150" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button
              v-if="row.status === 2"
              size="small"
              type="success"
              @click="completeTask(row)"
            >
              已完成
            </el-button>
            <el-button
              v-if="row.status === 2"
              size="small"
              type="warning"
              @click="abandonTask(row)"
            >
              放弃任务
            </el-button>
            <el-button
              v-if="row.status === 3"
              size="small"
              disabled
            >
              已完成
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '../../utils/api'

const loading = ref(false)
const tasks = ref([])

const getStatusText = (status) => {
  const statusMap = {
    0: '待解决',
    1: '已关闭',
    2: '解决中',
    3: '已完成'
  }
  return statusMap[status] || '未知'
}

const getStatusType = (status) => {
  const typeMap = {
    0: 'warning',
    1: 'info',
    2: 'primary',
    3: 'success'
  }
  return typeMap[status] || 'info'
}

const loadTasks = async () => {
  loading.value = true
  try {
    const response = await api.get('/user/tasks/accepted')
    if (response.data.code === 200) {
      tasks.value = response.data.data
    }
  } catch (error) {
    ElMessage.error('加载任务失败')
  } finally {
    loading.value = false
  }
}

const completeTask = async (task) => {
  try {
    await ElMessageBox.confirm('确定要完成这个任务吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    const response = await api.post(`/user/tasks/${task.id}/complete`)
    if (response.data.code === 200) {
      ElMessage.success('任务完成成功')
      loadTasks()
    } else {
      ElMessage.error(response.data.message)
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('任务完成失败')
    }
  }
}

const abandonTask = async (task) => {
  try {
    await ElMessageBox.confirm('确定要放弃这个任务吗？放弃后任务将重新开放给其他人接取。', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    const response = await api.post(`/user/tasks/${task.id}/abandon`)
    if (response.data.code === 200) {
      ElMessage.success('任务放弃成功')
      loadTasks()
    } else {
      ElMessage.error(response.data.message)
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('任务放弃失败')
    }
  }
}

onMounted(() => {
  loadTasks()
})
</script>

<style scoped>
.accepted-tasks {
  padding: 20px;
}
</style>